package com.atguigu.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ Author:任留辉
 * @ Description:
 * @ Date:Created in 23:03 2021/3/22 0022
 */
public class LoginFilter extends HttpFilter {

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if(user == null){
            //未登录
            request.setAttribute("errorMsg", "订单功能请先登录");
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }else{
            //已登录
            chain.doFilter(request, response);
        }
    }
}
